package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author sen
 * @since 2024-01-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);

        //2.查询小节
        QueryWrapper<EduVideo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapper1);

        //创建list集合，用于最终封装数据
        List<ChapterVo> finallList = new ArrayList<>();

        //3.遍历查询章节list集合进行封装
        //遍历查询章节list集合
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finallList.add(chapterVo);

            //4.遍历查询小节list集合，进行封装
//            List<ChapterVo> videoList = new ArrayList<>();
//            for (EduVideo eduVideo : eduVideoList) {
//                if (eduVideo.getChapterId().equals(eduChapter.getCourseId())) {
//                    ChapterVo chapterVo1 = new ChapterVo();
//                    BeanUtils.copyProperties(eduVideo, chapterVo1);
//                    videoList.add(chapterVo1);
//                }
//            }
//            chapterVo.setChildren(videoList);

            List<ChapterVo> chapterVos = findChild(eduChapter.getId(), eduVideoList);
            chapterVo.setChildren(chapterVos);
        }

        return finallList;
    }

    public List<ChapterVo> findChild (String Id, List<EduVideo> eduVideoList) {
        List<ChapterVo> videoList = new ArrayList<>();
        for (EduVideo eduVideo : eduVideoList) {
            if(Objects.equals(Id, eduVideo.getChapterId())) {
                ChapterVo chapterVo = new ChapterVo();
                BeanUtils.copyProperties(eduVideo, chapterVo);
                videoList.add(chapterVo);
            }
        }

        return videoList;
    }

    //删除章节，如果章节下含有小节，不进行删除
    @Override
    public boolean deleteChapter(String chapterId) {

        //根据chapterId章节id查询小节表，如果查询到数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if(count > 0) {
            throw new GuliException(20001, "不能删除，含有小节");
        } else {
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {

        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);

    }
}
